package mts.testframework.testitems;

import java.util.Date;
import java.util.TimerTask;

import mts.testframework.TestItemScheduler;


public abstract class TestItem extends TimerTask{

	private TestItem[] onCompleted;
	private long delay;
	protected Date startTime;
	protected Date endTime;
	
	public long getDeltaTime() {
		return endTime.getTime() - startTime.getTime();
	}
	
	protected Integer id = null;
	static int nextId = 0;
	
	protected byte car;
	
	public void setCar(byte car){
		this.car = car;
	}
	
	protected TestItemScheduler testItemScheduler;
	
	public void setId(int id) {
		this.id = id;
	}
	
	public long getDelay() {
		return delay;
	}
	
	@Override
	public void run() {
		start();
		if (id == null) {
			id = nextId++;
		}
		this.cancel();
	}
	
	public abstract void start();
	
	public TestItem(long delay, TestItem... onCompleted) {
		this.delay = delay;
		this.onCompleted = onCompleted;
	}
	
	public void complete() {
		for (int i = 0; i < onCompleted.length; i++) {
			onCompleted[i].setId(id);
			onCompleted[i].setCar(car);
			testItemScheduler.schedule(onCompleted[i]);
		}
		
		testItemScheduler.testItemCompleted();
	}

	public void setTestItemScheduler(TestItemScheduler testItemScheduler) {
		this.testItemScheduler = testItemScheduler;
		
	}
}
